package com.source.lightfowl.basedata.dao;

import com.source.lightfowl.basedata.entity.Poultry_Parent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Description:
 * @Author: Faink
 * @CreateTime: 2020/9/24 19:59
 */
@Transactional
public interface PoultryParentDao extends JpaRepository<Poultry_Parent,String>, JpaSpecificationExecutor<Poultry_Parent> {
    @Query(
            nativeQuery = true,
            value = "select `id` from `poultry_category_parent` where `poultryParentName` =?1 AND `isDelete`=0"
    )
    String findIdByName(String name);
    @Query(
            nativeQuery = true,
            value = "SELECT poultryParentName" +
                    " FROM `poultry_category_parent`" +
                    " WHERE id=?1" +
                    " AND `isDelete`=0"
    )
    String findNameById(String parentId);

    /**
     * 逻辑删除
     * @param id
     */
    @Modifying
    @Query(
            nativeQuery = true,
            value = "UPDATE `poultry_category_parent`" +
                    " SET `isDelete`=1" +
                    " WHERE id=?1"
    )
    void deleteById(String id);

    @Query(
            nativeQuery = true,
            value = "SELECT * " +
                    "FROM `poultry_category_parent` " +
                    "WHERE `isDelete`=0"
    )
    List<Poultry_Parent> findAll();

    @Query(
            nativeQuery = true,
            value = "SELECT * " +
                    " FROM `poultry_category_parent` " +
                    " WHERE `id`=?1" +
                    " AND `isDelete`=0"
    )
    Poultry_Parent findOne(String id);

    @Modifying
    @Query(
            nativeQuery = true,
            value = "UPDATE `poultry_category_parent`" +
                    " SET `poultryParentName`=?1" +
                    " WHERE `id`=?2" +
                    " AND `isDelete`=0"
    )
    void updateName(String name, String id);
}
